﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// CalendarDrawingObject.xaml 的交互逻辑
    /// </summary>
    public partial class CalendarDrawingObject : UserControl
    {
        public CalendarDrawingObject(Vant.CellTypes.Calendar calendar, IDrawingHelper drawingHelper)
        {
            var height = drawingHelper.GetImageCellSize().Height;
            var width = drawingHelper.GetImageCellSize().Width;

            if(height < 30 || width < 30)
            {
                InitializeComponent();
                return;
            }

            var mouthCount = (int)Math.Ceiling(height / 330);
            var selectionRectWidth = Math.Min(54, width / 7);
            var viewModel = new CalendarDrawingObjectViewModel();

            var weekdayHeader = Properties.Resources.Calendar_WeekdaysHeader.Split('|');
            var firstDay = int.Parse(calendar.firstDayOfWeek);
            viewModel.CalendarHeaders = weekdayHeader.Skip(firstDay).Concat(weekdayHeader.Take(firstDay)).ToList();

            viewModel.MonthText = DateTime.Now.ToString(Properties.Resources.Calendar_MonthHeaderFormat);
            viewModel.MonthTextVisibility = calendar.showSubtitle ? Visibility.Visible : Visibility.Collapsed;

            for (int i = 0; i < mouthCount; i++)
            {
                var current = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(i);
                var month = new MonthDetail();
                var dayCount = DateTime.DaysInMonth(current.Year, current.Month);
                var firstDayWeek = (int)current.DayOfWeek;
                var emptyDays = ((firstDayWeek - firstDay) + 7) % 7;
                for (int d = 0; d < emptyDays; d++)
                {
                    month.Dates.Add(new Day());
                }
                for (int d = 0; d < dayCount; d++)
                {
                    var currentDay = current.AddDays(d);
                    month.Dates.Add(new Day()
                    {
                        SelectionBrush = currentDay == DateTime.Today ? drawingHelper.GetBrush(calendar.color) : null,
                        SelectionRectWidth = selectionRectWidth,
                        Foreground = currentDay == DateTime.Today ? Brushes.White : Brushes.Black,
                        Text = currentDay.Day.ToString()
                    });
                }
                month.MonthWaterMark = calendar.showMark ? current.Month.ToString() : null;
                month.MonthTitle = current.ToString(Properties.Resources.Calendar_MonthHeaderFormat);
                month.MonthTitleVisibility = (i != 0 || !calendar.showSubtitle) ? Visibility.Visible : Visibility.Collapsed;
                month.DaysRowsCount = month.Dates.Count / 7 + (month.Dates.Count % 7 > 0 ? 1 : 0);
                viewModel.MonthDetail.Add(month);
            }

            this.DataContext = viewModel;
            InitializeComponent();
        }
    }

    public class CalendarDrawingObjectViewModel
    {
        public Visibility MonthTextVisibility { get; set; }
        public string MonthText { get; set; }
        public List<string> CalendarHeaders { get; set; } = new List<string>();
        public List<MonthDetail> MonthDetail { get; set; } = new List<MonthDetail>();
    }

    public class MonthDetail
    {
        public string MonthTitle { get; set; }
        public Visibility MonthTitleVisibility { get; set; }
        public string MonthWaterMark { get; set; }
        public int DaysRowsCount { get; set; }
        public List<Day> Dates { get; set; } = new List<Day>();
    }

    public class Day 
    {
        public string Text { get; set; }
        public double SelectionRectWidth { get; set; }
        public Brush SelectionBrush { get; set; }
        public Brush Foreground { get; set; } = Brushes.Black;
    }
}
